﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DesignLife.Admin.Business;

namespace DesignLife.Admin
{
    public partial class UserManager : System.Web.UI.Page
    {
        BusinessUser_Role bsUser = new BusinessUser_Role();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["AddUser"] = true;
                Session["AddRole"] = true;
                if (Session["Username"] != null)
                {
                    bindDataUser();
                    bindDataRole();
                }
                else
                {
                    Response.Redirect("AdminLogin.aspx");
                }
            }
        }

        private void bindDataRole()
        {
            DataTable data;
            if (Session["Role"] == "1")
            {
                data = bsUser.getRoleAll();
            }
            else
            {
                data = bsUser.getRoleWithoutAdmin();
            }
            gvRole.DataSource = data;
            gvRole.DataBind();
            ddlRole.DataSource = data;
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataValueField = "RoleID";
            ddlRole.DataBind();
        }

        private void bindDataUser()
        {
            DataTable data;
            if (Session["Role"] == "1")
            {
                data = bsUser.getUserAll();
            }
            else
            {
                data = bsUser.getUserWithoutAdmin();
            }
            gvUser.DataSource = data;
            gvUser.DataBind();
        }

        protected void gvUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["AddUser"] = false;
            GridViewRow row = gvUser.SelectedRow;
            int id = int.Parse(row.Cells[0].Text);
            ViewState["IDUser"] = id;
            string name = row.Cells[1].Text;
            txtUsername.Text = name; 
            if (id == 1 || name == "admin")
            {
                txtUsername.Enabled = false;
            }
            else
            {
                txtUsername.Enabled = true;
            }
            txtPassword.Visible = false;
            RequiredFieldValidator2.Visible = false;
            txtFullname.Text = row.Cells[2].Text;
            txtEmail.Text = row.Cells[3].Text;
            txtPhone.Text = row.Cells[4].Text;
            bindDataRole();
            ddlRole.Items.FindByText(row.Cells[5].Text).Selected = true;
            ckbStatus.Enabled = true;
            ckbStatus.Checked = Convert.ToBoolean(row.Cells[6].Text);
        }

        protected void gvRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["AddRole"] = false;
            GridViewRow row = gvRole.SelectedRow;
            int id = int.Parse(row.Cells[0].Text);
            ViewState["IDRole"] = id;
            string name = row.Cells[1].Text;
            txtRoleName.Text = name;
            if (id == 1 || name == "admin")
            {
                txtRoleName.Enabled = false;
            }
            else
            {
                txtRoleName.Enabled = true;
            }
            ckbRole.Enabled = true;
            ckbRole.Checked = Convert.ToBoolean(row.Cells[2].Text);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
            string userName = txtUsername.Text;
            string password = txtPassword.Text;
            string fullName = txtFullname.Text;
            string email = txtEmail.Text;
            string phone = txtPhone.Text;
            int roleID = int.Parse(ddlRole.SelectedItem.Value);
            bool status = ckbStatus.Checked;
            if(Session["AddUser"].Equals(true))
            {
                bsUser.AddUser(userName, password, fullName, email, phone, roleID);
                bindDataUser();
            }
            else
            {
                int id = (int)ViewState["IDUser"];
                bsUser.UpdateUser(fullName, email, phone, roleID, status, id);
                bindDataUser();
                Session["AddUser"] = true;
                txtPassword.Visible = true;
                RequiredFieldValidator2.Visible = true;
                ckbStatus.Enabled = false;
            }
            txtUsername.Text = "";
            txtPhone.Text = "";
            txtFullname.Text = "";
            txtEmail.Text = "";


        }

        protected void btnSubmitRole_Click(object sender, EventArgs e)
        {
            
            string roleName = txtRoleName.Text;
            bool status = ckbRole.Checked;
            if (Session["AddRole"].Equals(true))
            {
                bsUser.AddRole(roleName);
                bindDataRole();
            }
            else
            {
                int id = (int)ViewState["IDRole"];
                bsUser.UpdateRole(roleName, status, id);
                bindDataRole();
                Session["AddRole"] = true;
                ckbStatus.Enabled = false;
            }
            txtRoleName.Text = "";
            
        }

    }
}